{-----------------------------------------------------------------
 
  this module tests the CONTENT parsing facility

  (c) 2008 Markus Dittrich 
 
  This program is free software; you can redistribute it 
  and/or modify it under the terms of the GNU General Public 
  License Version 3 as published by the Free Software Foundation. 
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License Version 3 for more details.
 
  You should have received a copy of the GNU General Public 
  License along with this program; if not, write to the Free 
  Software Foundation, Inc., 59 Temple Place - Suite 330, 
  Boston, MA 02111-1307, USA.

--------------------------------------------------------------------}

module ContentParserTest ( content_tester ) where


-- basic imports
import qualified Data.ByteString.Char8 as BC
import Prelude
import System.FilePath.Posix((</>))
import System.IO

-- local imports
import Helpers.PrettyPrint
import TestHelpers
import Drivers.Content
import Parsers.Content


--
-- names and versions of test packages
--
category :: String
category = "test-category"

package :: String
package = "content-test-package"

version1 :: String
version1= "1.0.0"

version2 :: String
version2= "2.0.0"


--
-- reference data for package contents
--
contentPackage1 :: [PackageContentItem]
contentPackage1 = [
  defaultContent { fileType = Dir, fileName = BC.pack "/foo"},
  defaultContent { fileType = Dir, fileName = BC.pack "/foo/bar"},
  defaultContent { fileType = Obj, 
                         fileName = BC.pack "/foo/bar/stuff.sh", 
                         md5  = BC.pack "123",
                         size = BC.pack "456"}]


contentPackage2 :: [PackageContentItem]
contentPackage2 = [
  defaultContent { fileType = Dir, fileName = BC.pack "/one"},
  defaultContent { fileType = Dir, fileName = BC.pack "/one/two"},
  defaultContent { fileType = Obj, 
                         fileName = BC.pack "/one/two/three", 
                         md5  = BC.pack "661",
                         size = BC.pack "231"}]


testPackageContent :: [(String,[PackageContentItem])]
testPackageContent = [(package ++ "-" ++ version1, contentPackage1),
                      (package ++ "-" ++ version2, contentPackage2)]


------------------------------------------------------------------
-- end of section describing test and expected results
------------------------------------------------------------------

--
-- main driver to test parsing of content
--
content_tester :: IO Bool
content_tester = do
        -- retrieve database dir
        dbDir <- get_test_dbDir
        let path = dbDir ++ "/" ++ category

        contentResults <- mapM (\x -> check_content path (fst x) 
          (snd x) ) testPackageContent

        -- return status; if a single test failed False
        -- otherwise true
        return $ foldl (&&) True contentResults


--
-- checks the parsed content of a file against a reference data set
--
check_content :: FilePath -> String -> [PackageContentItem] -> IO Bool
check_content path packageName reference =
    
    do 
      -- short message
      putColorStr Cyan "Testing content of " 
      putColorStr White packageName
      putColorStr Cyan $ " ...."

      -- get content and compare it
      contents <- retrieve_package_content $ path </> packageName
      if (contents == reference)
        then do
          print_success
        else do
          print_failure
